package com.czy.workflow.core.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.czy.workflow.core.model.*;
import com.czy.workflow.core.service.FlowService;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
 * Created by admin on 2019/8/31.
 */


@Service("jsonFlowService")
public class JsonFlowService implements FlowService {

    Logger logger = Logger.getLogger(JsonFlowService.class.getName());

    private Map<String, FlowMain> flowMains = new HashMap<>();
    private Map<String, List> currentNodes = new ConcurrentHashMap<>();

    public JsonFlowService() {
        try {
            File file = ResourceUtils.getFile("classpath:json/F1.json");
            if(FileUtil.exist(file)){
                JSONObject json = JSONUtil.readJSONObject(file, Charset.forName("utf-8"));
                logger.info(json.toString());
                FlowMain flowMain = JSONUtil.toBean(json, FlowMain.class);
                flowMains.put(flowMain.getId(), flowMain);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean saveFlowMain(FlowMain flowMain) {
        return false;
    }

    @Override
    public FlowMain getFlowMainById(String flowId) {
        return flowMains.get(flowId);
    }

    @Override
    public FlowNode getFlowNodeById(String flowId, String nodeId) {
        FlowMain flowMain = getFlowMainById(flowId);
        for(FlowNode node : flowMain.getNodes()){
            if(nodeId.equals(node.getId())){
                return node;
            }
        }
        return null;
    }

    @Override
    public List<FlowNode> getNextNodes(String flowId, String nodeId) {
        List<FlowNode> nextNodes = new ArrayList<>();
        FlowMain flowMain = getFlowMainById(flowId);
        for(FlowBranch flowBranch : flowMain.getBranches()){
            if(nodeId.equals(flowBranch.getSourceNodeId())){
                nextNodes.add(getFlowNodeById(flowId, flowBranch.getTargetNodeId()));
            }
        }
        return nextNodes;
    }

    @Override
    public List<FlowBranch> getNodeBranches(String flowId, String nodeId) {
        List<FlowBranch> nodeBranches = new ArrayList<>();
        FlowMain flowMain = getFlowMainById(flowId);
        for(FlowBranch flowBranch : flowMain.getBranches()){
            if(nodeId.equals(flowBranch.getSourceNodeId())){
                nodeBranches.add(flowBranch);
            }
        }
        return nodeBranches;
    }

    @Override
    public List<FlowNode> getStartNodes(String flowId) {
        List<FlowNode> startNodes = new ArrayList<>();
        FlowMain flowMain = getFlowMainById(flowId);
        for(FlowNode flowNode : flowMain.getNodes()){
            if(OperationType.START.eq(flowNode.getOperationType())){
                startNodes.add(flowNode);
            }
        }
        return startNodes;
    }

    @Override
    public boolean refreshCurrentNodes(String flowId, String taskId, List<FlowNode> nodes) {
        currentNodes.put(flowId + "-" + taskId, nodes);
        return true;
    }

    @Override
    public boolean clearCurrentNodes(String flowId, String taskId) {
        currentNodes.remove(flowId + "-" + taskId);
        return false;
    }

    @Override
    public List<FlowNode> getCurrentNodes(String flowId, String taskId) {
        return currentNodes.get(flowId + "-" + taskId);
    }

    @Override
    public boolean saveFlowLog(TaskInfo taskInfo, boolean result) {
        return false;
    }

}
